package org.overtheair.say.domain;

import org.json.JSONException;
import org.json.JSONObject;
import org.openintents.intents.WikitudePOI;
import org.overtheair.say.common.GlobalBag;

public class DataUpcomingEvent extends DataAncestor {

	// ************************************************************************
	// ctor
	// ************************************************************************
	public DataUpcomingEvent() {
	}
	
	public DataUpcomingEvent(String jsonStrem)
	{
		buildFromJSON(jsonStrem);
	}
	
	// ************************************************************************
	// properties
	// ************************************************************************
	public long id;
	public static final String PROP_ID = "id";
	
	public String name;
	public static final String PROP_NAME = "name";
	public String description;
	public static final String PROP_DESCRIPTION = "description";
	
	public String uri;
	public static final String PROP_URI = "uri";
	public String event_url;
	public static final String PROP_EVENT_URL = "url";
	public String photo_url;
	public static final String PROP_PHOTO_URL = "photo_url";
	
	
	public String date_start_date;
	public static final String PROP_DATE_START_DATE = "start_date_last_rendition";
	public String date_start_time;
	public static final String PROP_DATE_START_TIME = "start_time";
	
	public double location_latitude;
	public static final String PROP_LOCATION_LATITUDE = "latitude";
	public double location_longitude;
	public static final String PROP_LOCATION_LONGITUDE = "longitude";
	public String location_woeid;
	public static final String PROP_LOCATION_WOEID = "woeid";
	
	public String venue_address;
	public static final String PROP_VENUE_ADDRESS = "venue_address";
	public String venue_city;
	public static final String PROP_VENUE_CITY = "venue_city";
	public String venue_name;
	public static final String PROP_VENUE_NAME = "venue_name";




	// ************************************************************************
	// interface implementation
	// ************************************************************************

	@Override
	public void buildFromJSON(String jsonStream) {
		JSONObject obj;
		
		try {
			obj = new JSONObject(jsonStream);
			
			this.id = obj.optLong(PROP_ID, GlobalBag.INVALID_DATA_KEY_LONG);
			this.name= obj.optString(PROP_NAME, "");
			this.description = obj.optString(PROP_DESCRIPTION, "");
			this.event_url = obj.optString(PROP_EVENT_URL, "");
			this.uri = obj.optString(PROP_URI, "");
			this.photo_url = obj.optString(PROP_PHOTO_URL, "");
			
			this.venue_address = obj.optString(PROP_VENUE_ADDRESS, "");
			this.venue_city = obj.optString(PROP_VENUE_CITY, "");
			this.venue_name = obj.optString(PROP_VENUE_NAME, "");

			this.date_start_date = obj.optString(PROP_DATE_START_DATE, "");
			this.date_start_time = obj.optString(PROP_DATE_START_TIME, "");
			
			location_latitude = obj.optDouble(PROP_LOCATION_LATITUDE, 0);
			location_longitude = obj.optDouble(PROP_LOCATION_LONGITUDE, 0);
			location_woeid = obj.optString(PROP_LOCATION_WOEID, "");

		} catch (JSONException e) {
			this.id = GlobalBag.INVALID_DATA_KEY_LONG;
		}
	}

	@Override
	public boolean isDataValid() {
		return id == GlobalBag.INVALID_DATA_KEY_LONG;
	}

	@Override
	public WikitudePOI toWikitudePOI()
	{
		WikitudePOI poi;
		String shortDesc;

		if (description.length() > 35)
			shortDesc = description.substring(0, 35) + "...";
		else
			shortDesc = description;

		poi = new WikitudePOI(
				location_latitude,
				location_longitude,
				0,
				name,
				date_start_date + ", " + venue_address + " - " + venue_city + "   " + shortDesc,
				uri,
				null,
				null,
				photo_url
				);
		return poi;
	}

}
